
package com.crowdfunding.circle.analyze.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.analyze.entity.ProjectTypeVO;
import com.crowdfunding.circle.analyze.param.ProjectTypeParam;
import com.crowdfunding.circle.analyze.service.ProjectTypeService;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;

/**
 * 项目类型对应的拆解项Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ProjectTypeController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(ProjectTypeController.class);
    
    @Autowired
    private ProjectTypeService projectTypeService;
    
    private final static String LIST_PAGE = "/admin/project_type_manage";
    
    private final static String UPDATE_PAGE = "/admin/project_type_edit";

    @RequestMapping(value = "/admin/projectType/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectTypeVO projectTypeVO = new ProjectTypeVO();
        resultMap.put("projectType", projectTypeVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/projectType/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectTypeVO projectTypeVO = projectTypeService.findById(id);
        resultMap.put("projectType", projectTypeVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/projectType/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(ProjectTypeVO projectTypeVO) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();

        String validMsg = projectTypeVO.valid();
        if (StringUtils.isNotBlank(validMsg)) {
            resultMap.put("result", false);
            resultMap.put("message", validMsg);
            return JSONObject.toJSONString(resultMap);
        }

        try {
            Integer insertCount = projectTypeService.update(projectTypeVO, seUser.getUserId());
            if (insertCount != null && insertCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改项目类型对应的拆解项成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改项目类型对应的拆解项失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改项目类型对应的拆解项出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改项目类型对应的拆解项出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/projectType/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = projectTypeService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除项目类型对应的拆解项成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除项目类型对应的拆解项失败");
            }
        } catch (Exception e) {
            logger.error("删除项目类型对应的拆解项出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除项目类型对应的拆解项出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/projectType/list")
    public ModelAndView queryList(ProjectTypeParam projectTypeParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ProjectTypeVO> dataList = projectTypeService.queryByPage(projectTypeParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", projectTypeParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
}
